package com.bdsoft.actions.netty.Controller;

import com.bdsoft.actions.AcUrls;
import com.bdsoft.actions.netty.MyServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/actions")
public class NettyController {
    @Autowired
    private MyServer nettyServer;

    @PostMapping("/start-netty")
    public ResponseEntity<String> startNetty() {
        try {
            new Thread(() -> {
                try {
                    nettyServer.setPort(18000);
                    nettyServer.run(); // 在独立线程中运行，不阻塞当前请求
                } catch (Exception e) {
                    // 记录日志（不要直接打印堆栈，建议接入日志框架）
                    System.err.println("Netty server startup failed: " + e.getMessage());
                    e.printStackTrace();
                }
            }).start();
            return ResponseEntity.ok("Netty server started successfully");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Failed to start Netty: " + e.getMessage());
        }
    }
}